#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _proc {
    char comm[0x40 + 2 + 1]; /* add another 2 for thread brackets */    // 72           // ==> 0x00
    char **argv;    // 8         /* only used : argv[0] is 1st arg; undef if argc < 1 */    // ==> 0x48
    int argc;       // 4         /* with -a   : number of arguments, -1 if swapped    */    // ==> 0x50
    pid_t pid;      // 4                // ==> 0x54
    pid_t pgid;     // 4                // ==> 0x58
    pid_t uid;      // 4                // ==> 0x5c
    ino_t ns[8];    // 64               // ==> 0x60
    char flags;     // 8                // ==> 0xA0
    double age;     // 8                // ==> 0xA8
    struct _child *children;    // 8    // ==> 0xB0
    struct _proc *parent;       // 8    // ==> 0xB8
    struct _proc *next;         // 8    // ==> 0xC0
} PROC;  

void test(PROC * this)
{
    /* 申请指针数据组空间 */
	if (!(this->argv = (char**)malloc(sizeof(char *) * 3))) {
        perror("malloc");
        exit(1);
    }
   
    /* argv[0] */
    if (!(this->argv[0] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv    = %p:%p\n", &this->argv, this->argv);             // 0x7fffd65d9de0:0x5604d873e2a0
    printf("this->argv[0] = %p:%p\n", &this->argv[0], this->argv[0]);       // 0x5604d873e2a0:0x5604d873e2c0

    memcpy(this->argv[0], "ac-test0", 8);
    printf("argv[0]=%s\n", this->argv[0]);
    
    /* argv[1] */
    if (!(this->argv[1] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv[1] = %p:%p\n", &this->argv[1], this->argv[1]);      // 0x5564a05f42a8:0x5564a05f4720
    memcpy(this->argv[1], "ac-test1", 8);
    printf("argv[0]=%s\n", this->argv[1]);
    
    /* argv[2] */
    if (!(this->argv[2] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv[2] = %p:%p\n", &this->argv[2], this->argv[2]);      // 0x5564a05f42b0:0x5564a05f4770
    memcpy(this->argv[2], "ac-test2", 8);
    printf("argv[0]=%s\n", this->argv[2]);
     
    /* argv[3] */
    if (!(this->argv[3] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }   
    printf("this->argv[3] = %p:%p\n", &this->argv[3], this->argv[3]);      // 0x5564a05f42b8:0x5564a05f47c0
    memcpy(this->argv[3], "ac-test3", 8);
    printf("argv[0]=%s\n", this->argv[3]); 
    
   	free(this->argv);
    free(this->argv[0]);
    free(this->argv[1]);
    free(this->argv[2]);
    free(this->argv[3]);
}

static void set_args(PROC * this, const char *args, int size)
{
    char *start;
    int i;

    if (!size) {
        this->argc = -1;
        return;
    }

    this->argc = 0;
    for (i = 0; i < size - 1; i++) {
        printf("for i = %d, args[i]=%c\n", i, args[i]);
        if (!args[i]) {
            this->argc++;
            printf("argc=%d\n", this->argc);
            /* now skip consecutive NUL */
            while(!args[i] && (i < size -1 )) {
                printf("while i = %d\n", i);
                i++;
            }
        }
    }

    if (!this->argc)
        return;

    if (!(this->argv = (char**)malloc(sizeof(char *) * this->argc))) {
        perror("malloc");
        exit(1);
    }
    
    start = strchr(args, 0) + 1;
    size -= start - args;
    if (!(this->argv[0] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    
    start = memcpy(this->argv[0], start, (size_t) size);
    for (i = 1; i < this->argc; i++)
        this->argv[i] = start = strchr(start, 0) + 1;
}

int main()
{
    PROC this = {0};
    char c = '0';
    
    // test(&this);

    if (!c) {
        printf("aaaaaaaaaaaaaaaaa\n");
    }

    this.argc = 6;
    set_args(&this, 0, 8);
    
    return 0;
}
